	var date=new Date;
	var year=date.getFullYear(); 
	$(document).ready(function () {
		
		 $('#old').text((year-1)+"营业时间："); $('#now').text(year+"营业时间："); $('#next').text((year+1)+"营业时间：");
		 
		
		 loadTable();
	})		
		
		
		
	function  loadTable()
	{	
		var columns=[[
		              {field:'proids',title:'产品ids',hidden:'true'},
		              {field:'proname',title:'产品名称',width:100,align:'center'},
		              {field:'oldyearpurchased',title:(year-1)+'年全年进货量',width:120,align:'center',
		            	  formatter: function(value,row,index)
		            	  {
		            		  if(value!=null){return  value;}
		            		  else{ return 0;}
		            	  }
		              },
		              {field:'oldmonthpurchased',title:(year-1)+'年月平均进货量',width:130,align:'center',
		            	  formatter: function(value,row,index)
		            	  {		
		            		  var oldtime=$('#oldNum').numberbox('getValue'); 
		            		  if(oldtime==0)
		            		  {
		            			  return 0;
		            		  }
		            		  if(row.oldyearpurchased!=null){
		            			  var a=row.oldyearpurchased/oldtime;
		            			  return  a.toFixed(2);}
		            		  else{ return 0;}
		            	  }
		              },
		              {field:'now9purchased',title:year+'年1-9月进货量',width:120,align:'center',
		            	  formatter: function(value,row,index)
		            	  {		
		            		  if(value!=null){return  value;}
		            		  else{ return row.now9purchased='0';}
		            	  }
		              },
		              {field:'now10willpurchased',title:year+'年10月预计进货量',width:150,align:'center',editor:'numberspinner',
		            	  formatter: function(value,row,index)
		            	  {		
		            		  if(value!=null){return  value;}
		            		  else{ return row.now10willpurchased='0';}
		            	  }
		              },
		              {field:'now11willpurchased',title:year+'年11月预计进货量',width:150,align:'center',editor:'numberspinner',
		            	  formatter: function(value,row,index)
		            	  {
		            		  if(value!=null){return  value;}
		            		  else{ return row.now11willpurchased='0';}
		            	  }
		              },
		              {field:'now12willpurchased',title:year+'年12月预计进货量',width:150,align:'center',editor:'numberspinner',
		            	  formatter: function(value,row,index)
		            	  {
		            		  if(value!=null){return  value;}
		            		  else{ return row.now12willpurchased='0';}
		            	  }
		              },
		              {field:'nowyearwillpurchased',title:year+'年全年预计进货量',width:140,align:'center',
		            	  formatter: function(value,row,index)
		            	  {	
		            		 var a =parseFloat(row.now9purchased)+parseFloat(row.now10willpurchased)+parseFloat(row.now11willpurchased)+parseFloat(row.now12willpurchased);
		            		 return row.nowyearwillpurchased=a;
		            	  }
		              },
		              {field:'nowmonthwillpurchased',title:year+'年月平均预计进货量',width:150,align:'center',
		            	  formatter: function(value,row,index)
		            	  {		
		            		  var nowtime=$('#nowNum').numberbox('getValue');
		            		  if(nowtime==0)
		            		  {
		            			  return 0;
		            		  }
		            		  var a=parseFloat(row.nowyearwillpurchased)/nowtime;
		            		 return row.nowmonthwillpurchased=a.toFixed(2);
		            	  }
		              },
		              
		              {field:'january',title:(year+1)+'年1月',width:70,align:'center',editor:'numberspinner',
		            	  formatter: function(value,row,index)
		            	  {
		            		  if(value!=null){return  value;}
		            		  else{ return row.february='0';}
		            	  }
		              },
		              {field:'february',title:(year+1)+'年2月',width:70,align:'center',editor:'numberspinner',
		            	  formatter: function(value,row,index)
		            	  {
		            		  if(value!=null){return  value;}
		            		  else{ return row.february='0';}
		            	  }
		              },
		              {field:'march',title:(year+1)+'年3月',width:70,align:'center',editor:'numberspinner',
		            	  formatter: function(value,row,index)
		            	  {
		            		  if(value!=null){return  value;}
		            		  else{ return row.march='0';}
		            	  }
		              },
		              {field:'april',title:(year+1)+'年4月',width:70,align:'center',editor:'numberspinner',
		            	  formatter: function(value,row,index)
		            	  {
		            		  if(value!=null){return  value;}
		            		  else{ return row.april='0';}
		            	  }
		              },
		              {field:'may',title:(year+1)+'年5月',width:70,align:'center',editor:'numberspinner',
		            	  formatter: function(value,row,index)
		            	  {
		            		  if(value!=null){return  value;}
		            		  else{ return row.may='0';}
		            	  }
		              },
		              {field:'june',title:(year+1)+'年6月',width:70,align:'center',editor:'numberspinner',
		            	  formatter: function(value,row,index)
		            	  {
		            		  if(value!=null){return  value;}
		            		  else{ return row.june='0';}
		            	  }
		              },
		              {field:'july',title:(year+1)+'年7月',width:70,align:'center',editor:'numberspinner',
		            	  formatter: function(value,row,index)
		            	  {
		            		  if(value!=null){return  value;}
		            		  else{ return row.july='0';}
		            	  }
		              },
		              {field:'august',title:(year+1)+'年8月',width:70,align:'center',editor:'numberspinner',
		            	  formatter: function(value,row,index)
		            	  {
		            		  if(value!=null){return  value;}
		            		  else{ return row.august='0';}
		            	  }
		              },
		              {field:'september',title:(year+1)+'年9月',width:70,align:'center',editor:'numberspinner',
		            	  formatter: function(value,row,index)
		            	  {
		            		  if(value!=null){return  value;}
		            		  else{ return row.september='0';}
		            	  }
		              },
		              {field:'october',title:(year+1)+'年10月',width:70,align:'center',editor:'numberspinner',
		            	  formatter: function(value,row,index)
		            	  {
		            		  if(value!=null){return  value;}
		            		  else{ return row.october='0';}
		            	  }
		              },
		              {field:'november',title:(year+1)+'年11月',width:70,align:'center',editor:'numberspinner',
		            	  formatter: function(value,row,index)
		            	  {
		            		  if(value!=null){return  value;}
		            		  else{ return row.november='0';}
		            	  }
		              },
		              {field:'december',title:(year+1)+'年12月',width:70,align:'center',editor:'numberspinner',
		            	  formatter: function(value,row,index)
		            	  {
		            		  if(value!=null){return  value;}
		            		  else{ return row.december='0';}
		            	  }
		              },
		              {field:'nextyearwillpurchased',title:(year+1)+'年全年预计进货量',width:150,align:'center',
		            	  formatter: function(value,row,index)
		            	  {		
		            		  var a=0+parseFloat(row.january)+parseFloat(row.february)+parseFloat(row.march)
		            		  		+parseFloat(row.april)+parseFloat(row.may)+parseFloat(row.june)
		            		  		+parseFloat(row.july)+parseFloat(row.august)+parseFloat(row.september)
		            		  		+parseFloat(row.october)+parseFloat(row.november)+parseFloat(row.december);
		            		 return row.nextyearwillpurchased=a;
		            	  }
		              },
		              {field:'nextmonthwillpurchased',title:(year+1)+'年月平均预计进货量',width:150,align:'center',
		            	  formatter: function(value,row,index)
		            	  {
		            		  var nexttime=$('#nextNum').numberbox('getValue');
		            		  if(nexttime==0)
		            		  {
		            			  return 0;
		            		  }
		            		  var a=parseFloat(row.nextyearwillpurchased)/nexttime;
			            		 return row.nextmonthwillpurchased=a.toFixed(2);
		            	  }
		              }
		              ]];
		
		$('#dudgetYear-datagrid').datagrid({
			  iconCls: 'icon-edit',
			  singleSelect:true,  
			  columns:columns,
			  toolbar: '#tb',
			  pagination: true,
			  pageSize: 10,
			  onClickCell: onClickCell
		});
		
	}
	function  ok()
	{	
		
		if($('#oldNum').numberbox('getValue')!="" && $('#nowNum').numberbox('getValue')!="" && $('#nextNum').numberbox('getValue')!="")
		{	
			$('#dudgetYear-datagrid').datagrid({
				url: "budgetYear/getBudget",
				queryParams: {
					'_query.oldYearStareTime':  (year-1)+"-01-01",
					'_query.oldYearEndTime':year+"-01-01",
//					'_query.oldYearOrganIds':$("#organIds").val(),
					'_query.nowYearStareTime': year+"-01-01",
					'_query.nowYearEndTime': year+"-10-01",
					'_query.nowYearOrganIds': $("#organIds").val(),
					'_query.budgetOrganIds':$("#organIds").val(),
					'_query.budgetYear':(year-1)+"",
					'_query.lcIds': $("#lcIds").val()
				},
			    method: 'post'
			})
//			var url="/budget/getBudget";
//			var data={"productIds":proIds};
//			var reData = common_ajax.ajaxFunc(url, data,"json");
//			
		}else
		{
			$.alert(parent.$, "请填写完整营业时间！");
		}
	}
	//单元格编辑
	var editIndex = undefined;
	$.extend($.fn.datagrid.methods, {
		editCell: function(jq,param){
			return jq.each(function(){
				var opts = $(this).datagrid('options');
				var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
				for(var i=0; i<fields.length; i++){
					var col = $(this).datagrid('getColumnOption', fields[i]);
					col.editor1 = col.editor;
					if (fields[i] != param.field){
						col.editor = null;
					}
				}
				$(this).datagrid('beginEdit', param.index);
				for(var i=0; i<fields.length; i++){
					var col = $(this).datagrid('getColumnOption', fields[i]);
					col.editor = col.editor1;
				}
			});
		}
	});
	function endEdit(){
		if (editIndex == undefined){return true}
		if ($('#dudgetYear-datagrid').datagrid('validateRow', editIndex)){
			$('#dudgetYear-datagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}
	function onClickCell(index, field){
//		if (endEdit()){
//			$('#dudgetYear-datagrid').datagrid('selectRow', index)
//					.datagrid('editCell', {index:index,field:field});
//			editIndex = index;
//		}
		if (editIndex != index){
			if (endEdit()){
				$('#dudgetYear-datagrid').datagrid('selectRow', index)
					.datagrid('beginEdit', index);
					editIndex = index;
			} else {
				$('#dudgetYear-datagrid').datagrid('selectRow', editIndex);
			}
		}
	}
	function save(){
		endEdit();
		var oldNum=$('#oldNum').val();
		var nowNum=$('#nowNum').val();
		var nextNum=$('#nextNum').val();
		var rows = $('#dudgetYear-datagrid').datagrid('getRows');
		var rowjson=JSON.stringify(rows);
		var url='/budgetYear/saveBudget';
		var data={"oldNum":oldNum,"nowNum":nowNum,"nextNum":nextNum,"budgetJson":rowjson};
		var reData = common_ajax.ajaxFunc(url,data);
		if(reData == "200")
		{
			 $.slideMsg("保存成功", parent.$);
			 parent.refresh();
		}else
		{
			$.alert(parent.$, reData);
		}
	}
	